<template>
  <div class="container-box">
    <p>停车管理：车位管理</p>
    <!--筛选表单-->
    <div class="filter-form">
      <el-form ref="form" label-width="180px" :inline="true" size="small" label-position="top">
        <el-row type="flex" justify="space-between">
          <el-form-item prop="community_code">
            <div>小区名称</div>
            <SelectView v-model="queryData.community_code" />
          </el-form-item>
          <el-form-item>
            <div>设备名称</div>
            <el-input v-model="queryData.name" placeholder="请输入" />
          </el-form-item>
          <el-form-item>
            <div>购买日期</div>
            <el-date-picker
              v-model="queryData.buy_time"
              style="width:200px"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="change"
            />
          </el-form-item>
          <el-form-item class="btn">
            <el-button @click="onSearchCancel">重置</el-button>
            <el-button type="primary" @click="onSearchSubmit">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!--操作区域-->
    <el-row type="flex" justify="space-between" class="tool-bar">
      <div />
      <div>
        <el-button type="primary" size="small" plain @click="dialogShow = true">新增通知公告</el-button>
      </div>
    </el-row>
    <!--表单区域-->
    <div class="table-box">
      <el-table :data="list" border stripe>
        <el-table-column prop="community.name" label="小区名称" />
        <el-table-column prop="community_code" label="设备编码" />
        <el-table-column prop="name" label="设备名称" />
        <el-table-column label="设备图片">
          <template slot-scope="{row}">
            <el-image v-if="row.photo" width="60px" :preview-src-list="srcList" :src="$webURL + row.photo" alt="" />
            <el-image
              v-else
              width="60px"
              src="http://124.223.14.236:8881/public/dist/static/loadfail.85045efc.png"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="brand" label="设备品牌" />
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="quantity" label="数量" />
        <el-table-column prop="buy_time" label="购买日期">
          <template slot-scope="{row}">
            <span>{{ moment(row.buy_time) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="durable_years" label="预计使用年限" />
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button size="small" type="primary" icon="el-icon-edit" circle @click="editRow(row.id)" />
            <el-popconfirm title="确定删除吗？" @confirm="delRow(row.id)">
              <el-button slot="reference" size="small" type="danger" icon="el-icon-delete" circle />
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页区域-->
    <el-row type="flex" justify="space-between" class="tool-bar">
      <div />
      <div class="page">
        <el-pagination
          :current-page.sync="queryData.current"
          :page-sizes="[4, 8, 10, 20]"
          :page-size="queryData.pageSize"
          layout="total, sizes, prev, pager, next,jumper"
          :total="total"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-row>
    <!-- 弹框区域 -->
    <el-drawer :title="formData.id ? '修改维修记录' : '添加维修记录'" size="40%" :visible.sync="dialogShow" @close="btnCancel">
      <div class="form_flex">
        <el-form ref="formLi" :model="formData" :rules="formRules" label-width="120px" inline size="normal">
          <el-row>
            <el-form-item label="所属小区" prop="community_code">
              <SelectView v-model="formData.community_code" />
            </el-form-item>
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="品牌" prop="brand">
              <el-input v-model="formData.brand" />
            </el-form-item>
            <el-form-item label="价格" prop="price">
              <el-input v-model="formData.price" />
            </el-form-item>
          </el-row>
          <el-row>
            <div>
              <el-form-item label="购买数量" prop="quantity">
                <el-input v-model="formData.quantity" />
              </el-form-item>
            </div>
            <div>
              <el-form-item label="购买时间" prop="buy_time">
                <template>
                  <el-date-picker v-model="formData.buy_time" type="date" placeholder="选择日期" @change="onChange" />
                </template>
              </el-form-item>
            </div>
          </el-row>
          <el-row>
            <el-form-item label="预计使用年限" prop="durable_years">
              <el-input v-model="formData.durable_years" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" type="textarea" />
            </el-form-item>
          </el-row>
          <el-form-item label="设备图片" prop="photo">
            <UploadImage v-model="formData.photo" />
          </el-form-item>
          <el-row type="flex" justify="end">
            <el-button size="small" type="primary" @click="btnOk">提交</el-button>
            <el-button size="small" @click="dialogShow = false">取消</el-button>
          </el-row>

        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { addUserApi, delDeviceApi, editUserApi, geDeviceListApi, editDeviceId } from '@/api/assets_index'
import SelectView from '../components/selectView.vue'
import UploadImage from '../components/uploadImage.vue'
import moment from 'moment'
export default {
  name: 'IndexDevice',
  components: { SelectView, UploadImage },
  data() {
    return {
      alltime: [],
      fileList: [],
      list: [],
      formData: {
        community: '', // 小区名称
        community_code: '',
        name: '', // 设备名
        code: '', // 编号
        photo: '', // 图片
        brand: '', // 品牌
        price: '', // 价格
        quantity: null, // 数量
        buy_time: null, // 购买时间
        durable_years: null, // 预计使用年限
        remark: ''// 备注
      },
      queryData: {
        buy_time: null,
        current: 1,
        pageSize: 5
      },
      formRules: {
        // name: [{
        //   required: true,
        //   message: '请填写',
        //   targger: 'blur'
        // }],
        // brand: [{
        //   required: true,
        //   targger: 'blur'
        // }],
        // price: [{
        //   required: true,
        //   message: '请填写',
        //   targger: 'blur'
        // }],
        // remark: [{
        //   required: true,
        //   message: '请填写',
        //   targger: 'blur'
        // }]
        // durable_years: [{
        //   required: true,
        //   message: '请填写',
        //   targger: 'change'
        // }],
        // buy_time: [{
        //   required: true,
        //   message: '请填写',
        //   targger: 'blur'
        // }],
        // quantity: [{
        //   required: true,
        //   message: '请填写',
        //   targger: 'blur'
        // }]
      },
      total: 0,
      dialogShow: false,
      time: '',
      msg: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 提交
    async btnOk() {
      await this.$refs.form.validate()
      console.log(this.formData.id)
      if (this.formData.id) {
        await editUserApi(this.formData)
      } else {
        await addUserApi(this.formData)
      }
      // this.formData.id ? await editUserApi(this.formData) : await addUserApi(this.formData)
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      this.dialogShow = false
      this.initData()
    },
    onChange(val) {
      this.formData.buy_time = moment(val).valueOf()
    },
    change(val) {
      const res1 = moment(val[0]).valueOf()
      const res2 = moment(val[1]).valueOf()
      this.alltime = [res1, res2].join(',')
    },
    // 获取页面数据
    async initData() {
      const res = await geDeviceListApi(this.queryData)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.count
      this.msg = res.msg
      this.srcList = res.data.rows.map(item => 'http://124.223.14.236:8881' + item.photo)
    },
    // 删除
    async delRow(id) {
      await delDeviceApi(id)
      this.$message.success(this.msg)
      if (this.queryData.current > 1 && this.list.length === 1) {
        this.queryData.current--
      }
      this.initData()
    },
    // 分页
    sizeChange(val) {
      this.queryData.pageSize = val
      this.initData()
    },
    currentChange(val) {
      this.queryData.current = val
      this.initData()
    },
    // 重置
    onSearchCancel() {
      this.queryData = {
        community_code: '',
        name: '',
        buy_time: null,
        durable_years: null,
        current: 1,
        pageSize: 5
      }
      this.$message.success('重置成功')
      this.initData()
    },
    // 查询
    onSearchSubmit() {
      this.queryData.buy_time = this.alltime
      this.queryData.current = 1
      this.initData()
    },
    // 回显
    async editRow(id) {
      const res = await editDeviceId(id)
      // console.log(res);
      this.formData = res.data
      this.dialogShow = true
    },
    // 关闭弹框
    btnCancel() {
      this.formData = {
        community: '', // 小区名称
        community_code: '',
        name: '', // 设备名
        code: '', // 编号
        photo: '', // 图片
        brand: '', // 品牌
        price: '', // 价格
        quantity: null, // 数量
        buy_time: null, // 购买时间
        durable_years: null, // 预计使用年限
        remark: ''// 备注
      }
      this.$refs.formLi.clearValidate()
      // console.log(111);
    },
    // 时间转化
    moment(time) {
      return moment(time).format('YYYY/MM/DD hh:mm:ss')
    }
  }
}
</script>
<style scoped>
.form_flex .el-row {
  display: flex;
}

.container-box {
  padding: 30px;
}

.filter-form,
.tool-bar {
  border: 1px solid #ddd;
  padding: 15px;
  margin-bottom: 10px;
}

.filter-form .el-form-item {
  width: 15%;
}

.el-input {
  width: 200px;
}

.el-select {
  width: 200px;
}
.btn{
  margin-top: 40px;
}
.el-drawer__title{
  padding: 0 30px;
}
</style>
